﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;


namespace ImageLibCS
{
    public class RectangleB:DrawItemBase
    {

        enum ControlState
        {
            Move,
            MoveP1,
            MoveP2,
            MoveP3,
            MoveP4
        }

        //四個控制點
        private Point p1, p2, p3, p4;
        private Rectangle m_CtlSizeRect1;
        private Rectangle m_CtlSizeRect2;
        private Rectangle m_CtlSizeRect3;
        private Rectangle m_CtlSizeRect4;

        ControlState m_CurrentState;        //目前控制點

        public RectangleB():this(0,0,50,50){}

        public RectangleB(int x, int y, int width, int height)
        {
            p1 = new Point(x, y);
            p2 = new Point(x + width, y);
            p3 = new Point(x + width, y + height);
            p4 = new Point(x, y + height);

            m_CtlSizeRect1 = new Rectangle(0,0,4,4);
            m_CtlSizeRect2 = new Rectangle(0, 0, 4, 4);
            m_CtlSizeRect3 = new Rectangle(0, 0, 4, 4);
            m_CtlSizeRect4 = new Rectangle(0, 0, 4, 4);
        }

        #region IDrawItem Members

        public override bool IsContains(System.Drawing.Point location)
        {
            return this.IsContains(location.X, location.Y);
        }

        public override bool IsContains(int x, int y)
        {
            System.Drawing.Drawing2D.GraphicsPath m_GraphicsPath = new System.Drawing.Drawing2D.GraphicsPath();                      
            Region m_Region = new Region();

            m_GraphicsPath.Reset();
            m_GraphicsPath.AddPolygon(new Point[] { p1, p2, p3, p4 });
            m_Region.MakeEmpty();
            m_Region.Union(m_GraphicsPath);

            return m_Region.IsVisible(x, y);


            Rectangle m_Rect = new Rectangle(10, 10, 100, 100);
            Rectangle m_CircleRect = new Rectangle(10, 20, 50, 120);
            Point[] p=new Point[]{
                new Point(30,40),
                new Point(50,40),
                new Point(50,60),
                new Point(30,60)
            };

            //System.Drawing.Drawing2D.GraphicsPath m_GraphicsPath = new System.Drawing.Drawing2D.GraphicsPath();
            //Region m_Region = new Region();

            //m_GraphicsPath.Reset();
            //m_GraphicsPath.AddRectangle(m_Rect);
            //m_GraphicsPath.AddEllipse(m_CircleRect);
            //m_GraphicsPath.AddPolygon(new Point[] { p1, p2, p3, p4 });

            //m_Region.MakeEmpty();
            //m_Region.Union(m_GraphicsPath);

            return m_Region.IsVisible(x, y);


        }

        public override bool IsCtlContains(System.Drawing.Point location)
        {
            if (m_CtlSizeRect1.Contains(location))
            {
                return true;
            }
            if (m_CtlSizeRect2.Contains(location))
            {
                return true;
            }
            if (m_CtlSizeRect3.Contains(location))
            {
                return true;
            }
            if (m_CtlSizeRect4.Contains(location))
            {
                return true;
            }
            if (this.IsContains(location))
            {
                return true;
            }
            return false;
        }

        public override bool IsCtlContains(int x, int y)
        {
            return this.IsCtlContains(new Point(x, y));
        }

        public override bool SetCurrentCtl(System.Drawing.Point location)
        {
            if (m_CtlSizeRect1.Contains(location))
            {
                m_CurrentState = ControlState.MoveP1;
                m_Cursor = Cursors.Cross;
                return true;
            }
            if (m_CtlSizeRect2.Contains(location))
            {
                m_CurrentState = ControlState.MoveP2;
                m_Cursor = Cursors.Cross;
                return true;
            }
            if (m_CtlSizeRect3.Contains(location))
            {
                m_CurrentState = ControlState.MoveP3;
                m_Cursor = Cursors.Cross;
                return true;
            }
            if (m_CtlSizeRect4.Contains(location))
            {
                m_CurrentState = ControlState.MoveP4;
                m_Cursor = Cursors.Cross;
                return true;
            }
            if (this.IsContains(location))
            {
                m_CurrentState = ControlState.Move;
                m_Cursor = Cursors.NoMove2D;
                return true;
            }
            return false;
        }

        public override void SetCtlOffset(int x, int y)
        {
            switch (m_CurrentState)
            {
                case ControlState.Move:
                    p1.X += x;
                    p1.Y += y;
                    p2.X += x;
                    p2.Y += y;
                    p3.X += x;
                    p3.Y += y;
                    p4.X += x;
                    p4.Y += y;
                    break;
                case ControlState.MoveP1:
                    p1.X += x;
                    p1.Y += y;

                    break;
                case ControlState.MoveP2:
                    p2.X += x;
                    p2.Y += y;

                    break;
                case ControlState.MoveP3:
                    p3.X += x;
                    p3.Y += y;

                    break;
                case ControlState.MoveP4:
                    p4.X += x;
                    p4.Y += y;
                    
                    break;
                default:
                    break;
            }
        }

        public override void Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            if (m_IsVisible)
            {
                e.Graphics.DrawPolygon(this.m_ColorPen, new Point[]{p1, p2, p3, p4});
                if (m_IsControlPointVisible)
                {
                    //控制點 p1
                    m_CtlSizeRect1.X = p1.X - 2;
                    m_CtlSizeRect1.Y = p1.Y - 2;
                    e.Graphics.DrawEllipse(this.m_ControlPointColorPen, m_CtlSizeRect1);//畫調整大小控制點 p1
                    //控制點 p2
                    m_CtlSizeRect2.X = p2.X - 2;
                    m_CtlSizeRect2.Y = p2.Y - 2;
                    e.Graphics.DrawEllipse(this.m_ControlPointColorPen, m_CtlSizeRect2);//畫調整大小控制點 p2
                    //控制點 p3
                    m_CtlSizeRect3.X = p3.X - 2;
                    m_CtlSizeRect3.Y = p3.Y - 2;
                    e.Graphics.DrawEllipse(this.m_ControlPointColorPen, m_CtlSizeRect3);//畫調整大小控制點 p3
                    //控制點 p4
                    m_CtlSizeRect4.X = p4.X - 2;
                    m_CtlSizeRect4.Y = p4.Y - 2;
                    e.Graphics.DrawEllipse(this.m_ControlPointColorPen, m_CtlSizeRect4);//畫調整大小控制點 p4
                }
            }
        }

        public override System.Drawing.Point Location
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override int Top
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override int Left
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override System.Drawing.Size Size
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override int Height
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override int Width
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }
}
